<?php
// @formatter:off
/**
 * @file Class.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-24 01:55:33 +0000 (Wed, 24 Jul 2013) $
 *
 * $Id: Class.php 99 2013-07-24 01:55:33Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/themes/mdefault/Class.php $
 */
// @formatter:on

require_once dirname(__FILE__)."/config.php";

$wcThemeMobileDefault = null;
if(!isset($wcDefaults["class-WCThemeMobileDefault"])) {
	$wcDefaults["class-WCThemeMobileDefault"] = array();
}
if(!isset($wcDefaults["class-WCThemeMobileDefault"]["templates"])) {
	$wcDefaults["class-WCThemeMobileDefault"]["templates"] = false;
}

class WCThemeMobileDefault extends WCTheme {
	protected $_buttons = array();

	//
	// Public Methods.
	public function addBarIcons($position, $id, $function, $icon) {
		$newButton = array(
			"id" => $id,
			"left" => "0",
			"func" => "${function}",
			"icon" => $icon
		);

		if(!isset($this->_buttons[$position])) {
			$this->_buttons[$position] = $newButton;
		} else {
			while($newButton) {
				$auxButton = $this->_buttons[$position];
				$this->_buttons[$position] = $newButton;
				$position++;

				if(isset($this->_buttons[$position])) {
					$newButton = $auxButton;
				} else {
					$this->_buttons[$position] = $auxButton;
					$newButton = false;
				}
			}
		}
	}
	//
	// Protected Methods.
	protected function load() {
		global $wcROOTDIR;
		global $wcROOTURI;
		global $wcThemeAssigns;
		global $wcVersions;
		global $wcAction;
		global $wcProfile;
		global $wcGroup;
		global $wcCollection;
		global $wcComic;
		global $wcDirectory;
		global $wcTag;
		global $wcRule;
		global $wcGroupsHolder;
		global $wcUser;
		global $wcThemeMobileDefault;
		global $wcDefaults;

		$wcThemeMobileDefault = $this;

		//
		// Setting theme basic information.
		$this->_name = "Default";
		$this->_themeRoot = dirname(__FILE__);
		$this->_themeUri = $wcROOTURI.substr($this->_themeRoot, strlen($wcROOTDIR));
		//
		// Setting theme template and parsers.
		$this->_parser = new WCParser();
		if($wcDefaults["class-WCThemeMobileDefault"]["templates"]) {
			$this->_parser->templatesRoot($wcDefaults["class-WCThemeMobileDefault"]["templates"]);
		} else {
			$this->_parser->templatesRoot("{$this->_themeRoot}/templates");
		}
		//
		// Checking if the group switching system should be activated.
		$groupsCount = 0;
		foreach($wcGroupsHolder->groups() as $group) {
			if($wcProfile->grantedGroup($group) && is_readable($group->root)) {
				$groupsCount++;
			}
		}
		if($groupsCount > 1) {
			$wcThemeAssigns["ENABLE-GROUPSWITCH"] = true;
		}
		//
		// Setting main template.
		if(defined("__SIMPLE_CONTAINER__")) {
			$this->_parser->load("/simplecontainer.html");
		} elseif(defined("__EMAIL__")) {
			$this->_parser->load("/email.html");
			require "{$this->_themeRoot}/controlers/email.php";
		} else {
			$this->_parser->load("/index.html");
			require "{$this->_themeRoot}/controlers/index.php";
		}

		$this->addBarIcons(0, "TopBarHome", "ReloadHome()", "{$wcROOTURI}/images/wcomix.png");
		$this->addBarIcons(1, "TopBarMenu", "window.location.href='?action=".WC_ACTION_MENU."&group='+GROUP_ID;", "{$this->_themeUri}/icons/mainmenu-icon.png");
		//$this->addBarIcons(99, "TopBarMobile", "SetMobile('".WC_SERVICE_MOBILE."',
		// false)", "{$this->_themeUri}/icons/mobile-icon.png");

		$wcThemeAssigns["THEME-URI"] = WCSanitizer::WComixPath($this->_themeUri);

		if($wcAction == WC_ACTION_NOTHING && !count($wcGroupsHolder->publicGroups())) {
			$wcAction = WC_ACTION_LOG_IN;
		}
		switch($wcAction) {
			case WC_ACTION_COMIC_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.comic.php";
				break;
			case WC_ACTION_COMIC_INFO:
				require "{$this->_themeRoot}/controlers/contents/c.comic-info.php";
				break;
			case WC_ACTION_TAG_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.tag.php";
				break;
			case WC_ACTION_TAG_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.tags.php";
				break;
			case WC_ACTION_SEARCH:
				require "{$this->_themeRoot}/controlers/contents/c.search.php";
				break;
			case WC_ACTION_COLLECTION_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.collections.php";
				break;
			case WC_ACTION_COLLECTION_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.collection.php";
				break;
			case WC_ACTION_LOG_IN:
				$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.login.html";
				break;
			case WC_ACTION_NOTHING:
			case WC_ACTION_LOGO:
				$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.logo.html";
				break;
			case WC_ACTION_CREDITS:
				if(!$wcDefaults["no-credits"]) {
					if($wcProfile->allowedTo(WC_PERM_SPECIAL_CREDIT)) {
						$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.credits.html";
						$wcThemeAssigns["ACTIONEXTERNALSTYLE"] = WCSanitizer::WComixPath("{$this->_themeUri}/templates/contents/c.credits.css");
					} else {
						$wcThemeAssigns["error-string"] = "You're not allowed to credits page";
						require "{$this->_themeRoot}/controlers/contents/c.403.php";
					}
				} else {
					require "{$this->_themeRoot}/controlers/contents/c.404.php";
				}
				break;
			//	case WC_ACTION_CANVAS:
			//		die("<h1>@CANVAS NOT IMPLEMENTED</h1>");
			//		//require "{$this->_themeRoot}/controlers/contents/c.resetuser.php";
			//		break;

			// Redirections:
			// This are pages not supported by this theme.
			// By default, these pages are redirected to menu.
			// @{
			case WC_ACTION_DIRECTORY_SHOW:
			case WC_ACTION_DIRECTORY_LIST:
			case WC_ACTION_RANDOM:
			case WC_ACTION_RULE_LIST:
			case WC_ACTION_RULE_SHOW:
			case WC_ACTION_USER_LIST:
			case WC_ACTION_USER_VIEW:
			case WC_ACTION_USER_EDIT:
			case WC_ACTION_USER_RESET:
			case WC_ACTION_USER_REGISTER:
			// @}
			case WC_ACTION_MENU:
				require "{$this->_themeRoot}/controlers/contents/c.menu.php";
				break;
			//	case WC_SACTION_COMIC_RENAME:
			//	case WC_SACTION_TAG_RENAME:
			//	case WC_SACTION_COLLECTION_RENAME:
			//	case WC_SACTION_RULE_RENAME:
			//	case WC_SACTION_DIRECTORY_RENAME:
			//		require "{$this->_themeRoot}/controlers/simplecontainer/s.editname.php";
			//		break;
			//	case WC_SACTION_COLLECTION_SET:
			//		require "{$this->_themeRoot}/controlers/simplecontainer/s.editcollection.php";
			//		break;
			//	case WC_SACTION_COMIC_TAGS:
			//		require "{$this->_themeRoot}/controlers/simplecontainer/s.edittags.php";
			//		break;
			//	case WC_SACTION_LOG_IN:
			//		require "{$this->_themeRoot}/controlers/simplecontainer/s.login.php";
			//		break;
			//	case WC_SACTION_GROUP_SWITCH:
			//		require "{$this->_themeRoot}/controlers/simplecontainer/s.switchgroup.php";
			//		break;

			// Fatal Exceptions:
			// This are pages not supported by this theme.
			// By default, these pages trigger a exception.
			// @{
			case WC_EACTION_REGISTER:
			case WC_EACTION_USERNAME:
			case WC_EACTION_PASSWORD:
				// @}
				trigger_error(__CLASS__." was not built to support action '{$wcAction}'.", E_USER_ERROR);
				break;

			case WC_PAGEERROR_403:
				require "{$this->_themeRoot}/controlers/contents/c.403.php";
				break;
			case WC_PAGEERROR_500:
				require "{$this->_themeRoot}/controlers/contents/c.500.php";
				break;
			case WC_PAGEERROR_404:
			default:
				require "{$this->_themeRoot}/controlers/contents/c.404.php";
				break;
		}

		$this->rebuildBarIcons();
	}
	protected function rebuildBarIcons() {
		global $wcThemeAssigns;

		$ids = array_keys($this->_buttons);
		sort($ids);

		$position = 0;
		foreach($ids as $posId) {
			$this->_buttons[$posId]["left"] = $position;
			$position += 30;
		}

		$wcThemeAssigns["bar-buttons"] = $this->_buttons;
	}
}

$wcThemeClasses['mdefault'] = "WCThemeMobileDefault";
?>
